package com.mongo.aggregation.controller;

import com.mongo.aggregation.model.dto.PeronDTO;
import com.mongo.aggregation.model.entity.PersonEntity;
import com.mongo.aggregation.model.entity.SalesEntity;
import com.mongo.aggregation.model.enums.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Queue;

/**
 * @author: qilongxin
 * @date: 2021/11/11 10:22
 */
@RestController
public class PersonController {

    @Autowired
    private MongoTemplate mongoTemplate;

    @GetMapping("/person")
    @ResponseBody
    public ServerResponse<List<PersonEntity>> findPersons() {
        List<PersonEntity> list = mongoTemplate.findAll(PersonEntity.class);
        return ServerResponse.ok(list);
    }

    @GetMapping("/sales")
    @ResponseBody
    public ServerResponse<List<SalesEntity>> findSales() {
        Query query = new Query();
        Criteria criteria = new Criteria();
        criteria.and(SalesEntity.Fields.items).exists(true);
        List<SalesEntity> salesEntities = mongoTemplate.find(query.addCriteria(criteria), SalesEntity.class);
        return ServerResponse.ok(salesEntities);
    }

    @GetMapping("/aggregation")
    @ResponseBody
    public ServerResponse<List<PeronDTO>> aggregation() {

        // 定义多个排序规则
        List<Sort.Order> orders = new ArrayList<>();
        orders.add(new Sort.Order(Sort.Direction.DESC, PersonEntity.Fields.age));
//        orders.add(new Sort.Order(Sort.Direction.ASC, PersonEntity.Fields.name));

        Aggregation aggregation = Aggregation.newAggregation(
                // 投影要查询的字段，排除id
                Aggregation.project(PersonEntity.Fields.name, PersonEntity.Fields.sex, PersonEntity.Fields.age),

                // 排序、分页
                Aggregation.sort(Sort.by(orders)),
                Aggregation.skip(0L),
                Aggregation.limit(20)
        );

        AggregationResults<PeronDTO> results = mongoTemplate.aggregate(aggregation, "person", PeronDTO.class);
        List<PeronDTO> mappedResults = results.getMappedResults();
        return ServerResponse.ok(mappedResults);
    }


    @GetMapping("/aggregation2")
    @ResponseBody
    public ServerResponse<List<PersonEntity>> aggregation2() {

        // 定义多个排序规则
        List<Sort.Order> orders = new ArrayList<>();
        orders.add(new Sort.Order(Sort.Direction.DESC, PersonEntity.Fields.age));
//        orders.add(new Sort.Order(Sort.Direction.ASC, PersonEntity.Fields.name));

        Aggregation aggregation = Aggregation.newAggregation(
                // 投影要查询的字段，排除id
                Aggregation.project(PersonEntity.Fields.name, PersonEntity.Fields.sex, PersonEntity.Fields.age),

                // 过滤出年龄大于20的文档
//                Aggregation.match(Criteria.where(PersonEntity.Fields.age).gte(20)),

                Aggregation.group(PersonEntity.Fields.name).first(PersonEntity.Fields.name).as(PeronDTO.Fields.name)
                        .first(PersonEntity.Fields.age).as(PeronDTO.Fields.age).first(PersonEntity.Fields.sex).as(PeronDTO.Fields.sexEnum),

                // 排序、分页
                Aggregation.sort(Sort.by(orders)),
                Aggregation.skip(0L),
                Aggregation.limit(20)
        );

        AggregationResults<PersonEntity> results = mongoTemplate.aggregate(aggregation, "person", PersonEntity.class);
        List<PersonEntity> mappedResults = results.getMappedResults();
        return ServerResponse.ok(mappedResults);
    }


}
